package com.hunt.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hunt.dao.StockBaseMapper;
import com.hunt.model.entity.StockBasic;
import com.hunt.model.entity.StockBasicSimple;
import com.hunt.service.StockBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @Author: sjs
 * @Date: 2020-9-16 16:55
 * @Description:
 */

@Service
@Transactional
public class StockBaseServiceImpl implements StockBaseService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private  final    Pattern num = Pattern.compile("^[0-9]*$");
    private  final    Pattern chi = Pattern.compile("[\u4e00-\u9fa5]");
    //原来只有查股票后面加的板块 仍然用原来的返回对象
    @Override
    public List<StockBasicSimple> findStockBaseByCode(String keyword) {

        //如果输入包含有数字
        List<String>keylike=null;
        Map stockbasemap=stringRedisTemplate.opsForHash().entries("Stock_Base_Message");
        Map blockmap=stringRedisTemplate.opsForHash().entries("Block_Info");
        stockbasemap.putAll(reconf(blockmap));
        //要么是纯数字要么后面有bj,sh,sz    300142  300142.sz 类
        if (hasnum(keyword)||iscodeend(keyword)){
            //查找map中所有相似代码
           keylike=getlikebykey(stockbasemap,keyword);
        }
        //如果有汉字  工业
        else if(haschinese(keyword)){
            keylike=getlikebyvalue(stockbasemap,keyword,"0");
        }
        //纯字母
        else {
            keylike=getlikebyvalue(stockbasemap,keyword,"1");
        }
        List<StockBasicSimple>result=new ArrayList<>();
        //用相似代码获取
        for(String key:keylike){
            StockBasicSimple sss=new StockBasicSimple();
            String str= String.valueOf(stockbasemap.get(key));
            JSONObject j1=JSON.parseObject(str);
            sss.setArea(j1.getString("area"));
            sss.setStockCode(j1.getString("symbol"));
            sss.setIndustry(j1.getString("industry"));
            sss.setTsCode(j1.getString("ts_code"));
            sss.setListDate(j1.getString("list_date"));
            sss.setName(j1.getString("name"));
            result.add(sss);
        }
         return result;
    }
    private   List<String> getlikebykey(Map<String, String> map, String keyword){
        List<String> list=new ArrayList<>();
        for (Map.Entry<String, String> entity : map.entrySet()) {
            if(entity.getKey().indexOf(keyword)>-1){
                list.add(entity.getKey());
            }

        }
        return list;
    }
    private   List<String> getlikebyvalue(Map<String, String> map, String keyword,String type){
        List<String> list=new ArrayList<>();
        if(type.equals("0")){
            for (Map.Entry<String, String> entity : map.entrySet()) {
                if(jsonconform1(entity.getValue()).indexOf(keyword)>-1){
                    list.add(entity.getKey());
                }

            }
        }
        if(type.equals("1")){
            for (Map.Entry<String, String> entity : map.entrySet()) {
                jsonconform2(entity.getValue());
                //防止汉字缩写没有
                try {
                    if(jsonconform2(entity.getValue()).indexOf(keyword)>-1){
                        list.add(entity.getKey());
                    }
                }catch (java.lang.NullPointerException e){}

            }
        }
        return list;
    }
    private  String jsonconform1(String value){
        JSONObject j1=JSON.parseObject(value);
        return j1.getString("name");

    }
    private  String jsonconform2(String value){
        JSONObject j1=JSON.parseObject(value);
        return j1.getString("cnabbr");

    }
    private boolean hasnum(String content) {
        boolean flag = false;
        Matcher m =num.matcher(content);
        if (m.matches()) {
            flag = true;
        }
        return flag;
    }
    private boolean iscodeend(String content) {
        String s=content.substring(content.length()-2);
        if(s.equals("sz")||s.equals("bj")||s.equals("sh")||s.equals("ti")){
            return  true;
        }
        else {
            return false;
        }
    }
    private boolean haschinese(String str) {
        Matcher m = chi.matcher(str);
        return m.find();
    }
    //把板块的哈希转成股票的哈希方便处理
    private Map reconf(Map<String, String> blockmap) {
        Map reconf=new HashMap();
        for (Map.Entry<String, String> entity : blockmap.entrySet()) {
            JSONObject jo=new JSONObject();
            JSONObject original=JSON.parseObject(entity.getValue());
            String code=original.getString("blockCode");
            jo.put("ts_code",code);
            jo.put("name",original.getString("blockName"));
            jo.put("cnabbr",original.getString("cnabbr"));
            jo.put("symbol",code.substring(0,code.length()-3));
            String s=JSON.toJSONString(jo);
            reconf.put(entity.getKey(),s);
        }
        return  reconf;
    }
}
